package io.gbznt.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.Query;
import io.gbznt.modules.sys.dao.SysFiletableDao;
import io.gbznt.modules.sys.entity.SysFiletableEntity;
import io.gbznt.modules.sys.entity.SysUserEntity;
import io.gbznt.modules.sys.service.SysFiletableService;
import io.gbznt.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("sysFiletableService")
public class SysFiletableServiceImpl extends ServiceImpl<SysFiletableDao, SysFiletableEntity>
		implements SysFiletableService {

	@Autowired
	private SysUserService userService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		// 文件的dis
		String getallid = (String) params.get("getallid");
		String[] ids = new String[]{};
		if(StringUtils.isNotEmpty(getallid)){
			ids = getallid.split(",");
		}
		// 文件备注
		String fileRealname = (String) params.get("fileRealname");
		// 文件备注
		String fileRemark = (String) params.get("fileRemark");
		IPage<SysFiletableEntity> page = this.page(new Query<SysFiletableEntity>().getPage(params),
				new QueryWrapper<SysFiletableEntity>()
						.in(ids.length > 0, "id", ids)
						.like(StringUtils.isNotBlank(fileRemark), "file_remark", fileRemark)
						.like(StringUtils.isNotBlank(fileRealname), "file_realname", fileRealname));

		for (SysFiletableEntity filetable : page.getRecords()) {
			SysUserEntity user = userService.getById(filetable.getCreateId());
			filetable.setCreateUser(user);
		}

		return new PageUtils(page);
	}

	/**
	 * 新增文件
	 * 
	 * @param
	 * 
	 * @return：
	 */
	@Override
	public String insertfile(SysFiletableEntity sysfile) {
		int bl = baseMapper.insertfile(sysfile);
		String id = null;
		if (bl > 0) {
			id = sysfile.getId();

		}

		return id;
	}

	/**
	 * 更新备注方法
	 * 
	 * @param
	 * 
	 * @return：
	 */
	@Override
	public String updatemark(String mark, List<String> ids) {
		// TODO Auto-generated method stub
		Map<String, Object> condition = new HashMap<>();
		condition.put("mark", mark);
		condition.put("ids", ids);
		String res = "";
		int i = baseMapper.updatemark(condition);
		if (i > 0) {
			res = "success";
		} else {
			res = "fail";
		}
		return res;
	}

	/**
	 * @Description: 获取文件列表
	 * @author : guoqiang
	 * @date : 2018年6月30日 下午3:01:56
	 * @param params
	 * @return
	 */
	@Override
	public List<SysFiletableEntity> queryList(Map<String, Object> params) {
		QueryWrapper<SysFiletableEntity> ew = new QueryWrapper<SysFiletableEntity>();
		String getallid = (String) params.get("getallid");
		if (StringUtils.isNotEmpty(getallid)) {
			ew.in(StringUtils.isNotBlank(getallid), "id", getallid);
		} else {
			ew.apply("1=2");
		}

		List<SysFiletableEntity> list = this.list(ew);

		return list;
	}

}
